/**
  Generated main.c file from MPLAB Code Configurator

  @Company
    Microchip Technology Inc.

  @File Name
    main.c

  @Summary
    This is the generated main.c using PIC24 / dsPIC33 / PIC32MM MCUs.

  @Description
    This source file provides main entry point for system initialization and application code development.
    Generation Information :
        Product Revision  :  PIC24 / dsPIC33 / PIC32MM MCUs - 1.171.4
        Device            :  PIC24FJ128GA705
    The generated drivers are tested against the following:
        Compiler          :  XC16 v2.10
        MPLAB 	          :  MPLAB X v6.05
*/

/*
    (c) 2020 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
*/

/**
  Section: Included Files
*/
#include "mcc_generated_files/system.h"
#include "mcc_generated_files/delay.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/i2c2.h"
#include "mcc_generated_files/MCP9808Drivers/MCP9808.h"
#include "mcc_generated_files/adc1.h"
/*
                         Main application
 */

void MAIN_RA7_SetInterruptHandler(void)
{
    printf("--7--\r\n");
}
void MAIN_RA10_SetInterruptHandler(void)
{
    printf("-10---\r\n");
}

//???????????????????????????????????
int normal_lgt =0;

int min_lgt = 0;
int MY_test = 0;
uint16_t finger_count = 0;
uint8_t finger_flag = 0;
 int maybe_have_finger(uint16_t nm_val ,uint16_t cu_val)
{
	if(cu_val <= (nm_val>>1) && (!finger_flag)){
		printf("[%s.%d]\r\n",__func__,__LINE__);
		finger_flag = 1;
		return 1;
	}
	return 0;
}
void  Tm_InterruptHandler(void)
{
	MY_test++;
	//printf("111111111111111111111\r\n");
	TMR1_Stop();
	finger_count = 0;
}
//almost 50ms
#define JISHU_MAX							10
#define  VALID_VAL_CNT						JISHU_MAX-2;
#define NORMAL_JISHU_MAX					200				//approximate 5s
#define NORMAL_THREAD						20				//approximate 5s
uint16_t nm_gather_err_cnt = 0;
int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    MCP9808_i2c_params ii2001 = {MCP9808_CLIENT_ADDRESS, MCP9808_BUS_ID};
    MCP9808_MODULE_Initialize(ii2001);
    MCP9600_alert_flags tecfalg;
    float temp001 ;
    IO_RA7_SetIOCInterruptHandler(MAIN_RA7_SetInterruptHandler);  
    IO_RA10_SetIOCInterruptHandler(MAIN_RA10_SetInterruptHandler);  
    int cout = 0;
	
    uint8_t deviceId01; 
    uint8_t deviceRevision001;
    MCP9808_GetDeviceID(ii2001, &deviceId01, &deviceRevision001);
    printf("----------deviceId0 [%d] deviceRevision001[%d] \r\n",deviceId01,deviceRevision001);
	TMR1_SetInterruptHandler(Tm_InterruptHandler);
    //TMR1_Start();
    //ADC1_Start();
    uint16_t light_val = 0;
    uint16_t light_val_cu = 0; 
	uint16_t light_val_nm = 0;		//normal val
	
    uint16_t light_val_min = 0xffff;  
    uint16_t light_val_max = 0;
    
    double lght_v;
    double lght_c;
    uint16_t val[8];
    uint16_t count = 0;
    while (1)
    {
		IO_RB4_SetHigh();
       // printf("--cout[%d]---\r\n",cout);
        DELAY_milliseconds(2);
        
      //  DELAY_milliseconds(1510L);
        
        // Add your application code
       // MCP9808_GetTemperatureValue(ii2001,&tecfalg,&temp001);
       // printf("--temp001[%f]---\r\n",temp001);
       // printf("2--cout[%d]---\r\n",cout);
        light_val = ADC1_GetConversion(channel_AN8);
        //lght_v = (3.3*light_val)/0x3ff;
       // lght_c = lght_v*100
         count++;
         light_val_cu += light_val;
        if(light_val < light_val_min){
            light_val_min = light_val;
        }
         if(light_val_max < light_val){
            light_val_max = light_val;
        }
        
        if(JISHU_MAX <= count){
            light_val_cu -= (light_val_min + light_val_max);
            light_val_cu = light_val_cu>>3;					//do not change other vall ,this code is 
			
			if(((light_val_max <(light_val_nm+NORMAL_THREAD))&&((light_val_nm - NORMAL_THREAD) < light_val_max)) || (NORMAL_JISHU_MAX < nm_gather_err_cnt)){
				nm_gather_err_cnt =0;
				light_val_nm = (light_val_nm + light_val_max)>>1;
				if(finger_flag){
					finger_flag = 0;
					if(!finger_count){
						TMR1_Start();
					}
					finger_count++;
					printf("finger_count[%d]MY_test[%d]\r\n",finger_count,MY_test);
				}
			}else{
				nm_gather_err_cnt++;
			}
			//printf("MY_test[%d]light[%d:%d]\r\n",MY_test,light_val_max,light_val_nm);
			maybe_have_finger(light_val_nm,light_val_max);
            light_val_cu = 0;
            light_val_min = 0xffff;
            light_val_max = 0;
			count = 0;	
        }
        
	    IO_RB4_SetLow();
    }
    return 1;
}
/**
 End of File
*/

